package com.flow.asyncflow.core.node;


import com.flow.asyncflow.core.entity.config.AsyncFlowInfo;
import com.flow.asyncflow.core.entity.core.*;
import com.flow.asyncflow.core.enums.ExecuteEnum;
import com.flow.asyncflow.core.utils.log.asyncflowlog.AsyncFlowCommon_Log;
import com.flow.asyncflow.core.utils.msg.AsyncFlowSendMsg;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

public class FlowNodeTask implements Callable<AsyncFlowNodeResponse> {

    private IFlowNode flowNode;
    //请求参数
    private AsyncFlowRequest asyncFlowRequest;
    //上下文
    private AsyncFlowContext asyncFlowData;
    private AsyncFlowInfo asyncFlowInfo;
    private boolean isAsync;
    private ExecuteEnum executeEnum;

    public FlowNodeTask(IFlowNode flowNode, AsyncFlowRequest asyncFlowRequest, AsyncFlowContext asyncFlowContext, boolean isAsync, AsyncFlowInfo asyncFlowInfo, ExecuteEnum executeEnum) {
        this.flowNode = flowNode;
        this.asyncFlowRequest = asyncFlowRequest;
        this.asyncFlowData = asyncFlowContext;
        this.isAsync = isAsync;
        this.asyncFlowInfo = asyncFlowInfo;
        this.executeEnum = executeEnum;
    }

    @Override
    public AsyncFlowNodeResponse call() {
        //Thread.currentThread().setName(asyncFlowRequest.getAsyncFlowNodeName());
        return flowNodeTaskExecute();
    }

    public AsyncFlowNodeResponse flowNodeTaskExecute() {
        AsyncFlowNodeResponse asyncFlowNodeResponse = new AsyncFlowNodeResponse();
        Date startTime = new Date();
        try {
            asyncFlowNodeResponse = flowNode.execute(asyncFlowData);
            setData(executeEnum, asyncFlowNodeResponse, asyncFlowData);
            //失败发送消息给节点负责人
            if (asyncFlowNodeResponse.isSendNodeFailAlarmMsg() && asyncFlowNodeResponse.getCode() != 200) {
                AsyncFlowSendMsg.sendAlarmMsg(asyncFlowRequest, asyncFlowNodeResponse.getMsg(), flowNode.getClass().getSimpleName(), asyncFlowInfo);
            }
            //发送业务报警
            if (asyncFlowNodeResponse.isSendBusinessAlarmMsg()) {
                AsyncFlowSendMsg.sendBusinessAlarmMsg(asyncFlowRequest, asyncFlowNodeResponse.getMsg(), flowNode, asyncFlowInfo);
            }
            AsyncFlowCommon_Log.AddAsyncFlowLog(asyncFlowRequest,asyncFlowNodeResponse, asyncFlowData, flowNode, startTime, executeEnum, isAsync);
            return asyncFlowNodeResponse;
        } catch (Exception ex) {
            asyncFlowNodeResponse.setCode(-1);
            asyncFlowNodeResponse.setMsg(ex.toString());
            String name = flowNode.getClass().getSimpleName();
            String loadstartName = "执行" + name + "异常";
            String s = AsyncFlowSendMsg.buildErrorMsg(asyncFlowRequest, ex, name, asyncFlowInfo);
            AsyncFlowSendMsg.sendQYWXMsg(asyncFlowInfo.getFlowMaintainName(), s, asyncFlowInfo.getNodeArmUrl());
            AsyncFlowCommon_Log.AddAsyncFlowDetailLog(asyncFlowData, name, loadstartName, ex.toString(), asyncFlowNodeResponse.getMsg() + "##" + s, false);
            AsyncFlowCommon_Log.AddAsyncFlowLog( asyncFlowRequest,asyncFlowNodeResponse, asyncFlowData, flowNode, startTime, executeEnum, isAsync);
            return asyncFlowNodeResponse;
        }
    }

    private void setData(ExecuteEnum executeEnum, AsyncFlowNodeResponse asyncFlowNodeResponse, AsyncFlowBaseContext asyncData) {
        if (asyncFlowNodeResponse.getDataPub() == null || asyncFlowNodeResponse.getDataPub().isEmpty() || executeEnum != ExecuteEnum.GetData) {
            return;
        }
        for (Map.Entry<String, Object> entry : asyncFlowNodeResponse.getDataPub().entrySet()) {
            if (entry.getKey() == null || entry.getValue() == null) {
                continue;
            }
            asyncData.setData(entry.getValue());
        }
        asyncFlowNodeResponse.setDataPub(new HashMap<>());

    }
}
